/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.example.threadlocaltest.demos.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.threadlocaltest.demos.configure.TokenUtils;
import org.example.threadlocaltest.demos.configure.UnifiedReturn;
import org.example.threadlocaltest.demos.eneity.domain.MyStudent;
import org.example.threadlocaltest.demos.eneity.service.impl.MyStudentServiceImpl;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.List;

@RestController
@RequestMapping("/basic")
public class BasicController {

    @Autowired
    private MyStudentServiceImpl myStudentService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private DataSource dataSource;


    @GetMapping("/hello")
    public UnifiedReturn<Boolean> hello() {
        String token = TokenUtils.getToken();//该方法想在哪里用都可以，只要在同一个线程中 都能获取到 token
        System.out.println("token: " + token);
        MyStudent myStudent = new MyStudent();
        myStudent.setAge(18);
        myStudent.setName("张三");
        myStudent.setTeacherId(1);
        boolean save = myStudentService.save(myStudent);
        return UnifiedReturn.success(save);
    }

    @GetMapping("/findAll")
    public UnifiedReturn<List<MyStudent>> findAll() {
        List<MyStudent> list = myStudentService.list();
        return UnifiedReturn.success(list);
    }

    //测试异常处理--这里测试的是 运行时异常
    @GetMapping("/exception")
    public UnifiedReturn<String> exception() {
        int i = 1 / 0;
        return UnifiedReturn.success("success");//不会执行到这里
    }

    @GetMapping("/exception2")//这里测试的是 主动抛出的异常
    public UnifiedReturn<String> test() {
        throw new RuntimeException("test exception");
    }

    @GetMapping("/page")
    public UnifiedReturn<Page<MyStudent>> page(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        Page<MyStudent> myStudentPage = new Page<>(pageNo, pageSize);
        Page<MyStudent> page = myStudentService.findByPage(myStudentPage);
        List<MyStudent> records = page.getRecords();
        return UnifiedReturn.success(page);
    }

    @GetMapping("/testRedis")
    public UnifiedReturn<String> testRedis() {
        stringRedisTemplate.opsForValue().set("test", "test");
        return UnifiedReturn.success("success");
    }

    @GetMapping("/testRedisson")
    public UnifiedReturn<String> testRedisson() {
        //设置 key=test1  value=你好
        redissonClient.getBucket("test1").set("你好");
        return UnifiedReturn.success("success");
    }


    @GetMapping("/testRedisson2")
    public UnifiedReturn<String> testRedisson2() {
        //获取 key=test1 的值
        String test1 = redissonClient.getBucket("test1").get().toString();
        return UnifiedReturn.success(test1);
    }

    @GetMapping("/testDataSource")
    public UnifiedReturn<String> testDataSource() {
        return UnifiedReturn.success(dataSource.toString());
    }

}
